finp$="p4572654wb.png"

fout$=finp$+"_.bmp"
'- 0108091823
'---- teste1_0105052418_fileopening_msx1_8x1_conv.txt
'---- 010808 - primeira tentativa no firstbasic-dos

'- reads png file

halftm=1

xed=0:yed=0:open finp$ for input as #1:for i=1 to 16:n=readbyte(1):next:for i=1 to 2:n=readbyte(1):next
xed=readbyte(1)*256:xed=xed+readbyte(1):for i=1 to 2:n=readbyte(1):next:yed=readbyte(1)*256:yed=yed+readbyte(1)
close #1

setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls:Loadimage(finp$,1):pasteicon(0,0,1)
dim npaletr[16],npaletg[16],npaletb[16],clust[8,8],grpltlev[16],realpal[16]
dim rfromat[16],gfromat[16],bfromat[16],r0[8,8],g0[8,8],b0[8,8],plte[16]
dim binarydump[16384]

'- defines the cluster for halftone
dmst$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12."

if halftm=1 then:
  dmst$="00,06,09,15,02,12,05,11,08,14,01,07,04,10,03,13."
  end if

if halftm=2 then:
  dmst$="00,10,01,11,04,14,05,15,08,02,09,03,12,06,13,07."
  end if

for y2=0 to 3:for x2=0 to 3:clust[x2,y2]=val(mid$(dmst$,((y2*4+x2)*3)+1,2)):next:next

'- colour palette
dmst$="0,1,0,0,5,7,3,4,2,4,0,1,7,7,7,7,":for i=0 to 15:rr=val(mid$(dmst$,(i*2)+1,1)):npaletr[i]=int((rr*255)/7):next
dmst$="0,0,4,4,0,0,4,4,2,4,7,7,2,4,7,7,":for i=0 to 15:rg=val(mid$(dmst$,(i*2)+1,1)):npaletg[i]=int((rg*255)/7):next
dmst$="0,7,0,7,0,7,0,4,2,7,0,7,2,7,0,7,":for i=0 to 15:rb=val(mid$(dmst$,(i*2)+1,1)):npaletb[i]=int((rb*255)/7):next

for i=0 to 15:grpltlev[i]=int(((npaletr[i]*30)+(npaletg[i]*59)+(npaletb[i]*11))/100): next
for i=0 to 15:print grpltlev[i]:next
for i=0 to 15:plte[i]=rgb(npaletr[i],npaletg[i],npaletb[i]):next

xmax=xed:ymax=yed
xmaxo=int(xmax/8):ymaxo=int(ymax/8)
xmaxoo=xmaxo*8:ymaxoo=ymaxo*8
xsm0=(xmaxoo and 255):ysm0=(ymaxoo and 255)

for i=0 to 15:rfromat[i]=npaletr[i]:gfromat[i]=npaletg[i]:bfromat[i]=npaletb[i]:next
for y1=0 to ymaxo-1
  setcaption(str$(int((y1*100)/ymaxo))+"%")
  for x1=0 to xmaxo-1
    for y2=0 to 7
      for x2=0 to 7
        y=y1*8+y2:x=x1*8+x2:iy=(ymax-1)-y:ympos=(((iy*xmax)+x)*3)+55
        zzz=point(x,y)
        b0[x2,y2]=bitwiseand(zzz,255):zzz=int(zzz/256)
        g0[x2,y2]=bitwiseand(zzz,255):zzz=int(zzz/256)
        r0[x2,y2]=bitwiseand(zzz,255)
        next:next
    for y2=0 to 7
      y=y1*8+y2
      bi=0:ri=0:gi=0
      for x2=0 to 7
        x=(x1*8)+x2:iy=(ymax-1)-y:bi=bi+b0[x2,y2]:gi=gi+g0[x2,y2]:ri=ri+r0[x2,y2]:
        next
      b=bi/8:g=gi/8:r=ri/8:dbuf=1000:paattr=0:ikattr=15:pa=0:ik=15
      lumik=1:lumpa=0
      for pa=0 to 14
        for ik=pa+1 to 15
          graypa=grpltlev[pa]: grayik=grpltlev[ik]
          '- lumik=grayik:lumpa=graypa
          grayrgb=(((b*11)+(r*30)+(g*59))/100)
          if grayrgb>graypa and grayrgb<grayik then
            ikincid=((grayrgb-graypa)*255)/(grayik-graypa)
            rfikinc=((rfromat[ik]*ikincid)+(rfromat[pa]*(255-ikincid)))/255
            gfikinc=((gfromat[ik]*ikincid)+(gfromat[pa]*(255-ikincid)))/255
            bfikinc=((bfromat[ik]*ikincid)+(bfromat[pa]*(255-ikincid)))/255
            rdist=abs(rfikinc-r):gdist=abs(gfikinc-g):bdist=abs(bfikinc-b)
            rgbdist=sqr((rdist^2)+(gdist^2)+(bdist^2))
            if rgbdist<=dbuf then:dbuf=rgbdist:paattr=pa:ikattr=ik:lumik=grayik:lumpa=graypa:end if
            end if
          next:next

      '- memory block 4 reading as grayscale for screen 2
      dflum=lumik-lumpa:pkvar=0
      dpou=0
      for x2=0 to 7
        x=x1*8+x2:yi=(ymaxoo-1)-y:b=b0[x2,y2]:g=g0[x2,y2]:r=r0[x2,y2]
        vlue=(((b*11)+(r*30)+(g*59))/100)
        patgf1=x2 mod 4:patgf2=y mod 4:patgf=(((clust[patgf1,patgf2]+1)*255)/16)
        varnd=((patgf*dflum)/255)+lumpa
        ik=ikattr
        if varnd>vlue then:
          ik=paattr
          dpou=bitwiseor(dpou,2^(x2))
          end if
        ink(plte[ik]):dot(x,y)
        next
      binarydump[(y*40)+x1]=dpou
      binarydump[7680+(y*40)+x1]=(paattr*16)+ikattr
      next:next:next

grab (1,0,0,xed,yed):saveimage(fout$,1)
shell("convert "+fout$+" "+finp$+"_.png"):shell("rm "+fout$)

open finp$+".bin" for output as #2
for ee=0 to 15359
  writebyte(2,binarydump[ee])
  next
close #2








